from django import forms
from django.shortcuts import render
from django.core.validators import RegexValidator
from app01 import models


class RegisterModelForm(forms.ModelForm):
    # 自定义字段，更改原字段属性
    mobile_phone = forms.CharField(
        label="手机号",
        validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9})\d{9}'),"手机格式错误",])
    password = forms.CharField(label="密码",widget=forms.PasswordInput())
    # 增加字段
    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput())
    code = forms.CharField(label="验证码")

    class Meta:
        model = models.UserInfo
        # 获取模型类定义的所有字段
        fields = ['username','email','password','confirm_password','mobile_phone','code']

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        for name,field in self.fields.items():
            # 批量添加样式
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = '请输入{}'.format(field.label)

def register(request):
    form = RegisterModelForm()
    return render(request,'app01/register.html',{"form":form})

